﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace HYRElectric.Equipment
{
	public partial class FireDetectorLayoutForm : System.Windows.Forms.Form
	{
		public FireDetectorLayoutForm()
		{
			this.InitializeComponent();
		}

		private void FireDetectorLayoutForm_Load(object sender, EventArgs e)
		{
			this.InitForm();
			this.ReadFormDataFromXml();
		}

		private void InitForm()
		{
			//base.Icon = Resource.YArchitechIcon;
			this.InitPictureBox();
		}

		private void InitPictureBox()
		{
			string text = Path.Combine(Product.InstallLocation, "EquipmentImage", "ElectricLegend", "烟感探测器.png");
			if (!File.Exists(text))
			{
				this.pictureSmoke.Image = this.pictureSmoke.InitialImage;
			}
			else
			{
				this.pictureSmoke.Image = Image.FromFile(text);
			}
			text = Path.Combine(Product.InstallLocation, "EquipmentImage", "ElectricLegend", "温感探测器.png");
			if (!File.Exists(text))
			{
				this.pictureThermal.Image = this.pictureThermal.InitialImage;
				return;
			}
			this.pictureThermal.Image = Image.FromFile(text);
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\FireDetector";
			xmlHelper.SelectNode(path);
			if (Convert.ToInt32(xmlHelper.GetAttributeEx("DetectorType", "0")) == 0)
			{
				this.radioSmoke.Checked = true;
			}
			else
			{
				this.radioThermal.Checked = true;
			}
			if (Convert.ToInt32(xmlHelper.GetAttributeEx("LayoutWay", "0")) == 0)
			{
				this.radioRandom.Checked = true;
				return;
			}
			this.radioRect.Checked = true;
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\FireDetector";
			xmlHelper.SelectNode(path);
			xmlHelper.SetAttribute("DetectorType", ((int)this.DetectorType).ToString());
			xmlHelper.SetAttribute("LayoutWay", this.LayoutWay.ToString());
			xmlHelper.SaveXml("");
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.SaveFormDataToXml();
			base.DialogResult = DialogResult.OK;
		}

		public FireDetectorType DetectorType
		{
			get
			{
				if (!this.radioSmoke.Checked)
				{
					return FireDetectorType.Detector_Thermal;
				}
				return FireDetectorType.Detector_Smoke;
			}
		}

		public int LayoutWay
		{
			get
			{
				if (!this.radioRandom.Checked)
				{
					return 1;
				}
				return 0;
			}
		}

		private const string XmlNode_Root = "FireDetector";

		private const string XmlNode_DetectorType = "DetectorType";

		private const string XmlNode_LayoutWay = "LayoutWay";
	}
}
